package com.inspinia.base.util;
/**
 * Created by veblen on 2017/3/18.
 */

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PhoneNumberUtil {

    //用于匹配固定电话号码

    //用于获取固定电话中的区号
    private final static String REGEX_ZIPCODE = "^(010|02\\d|0[3-9]\\d{2})\\d{6,8}$";

    private static Pattern PATTERN_MOBILEPHONE;
    private static Pattern PATTERN_FIXEDPHONE;
    private static Pattern PATTERN_ZIPCODE;

    static {
        PATTERN_FIXEDPHONE = Pattern.compile(RegexConstants.REGEX_TEL);
        PATTERN_MOBILEPHONE = Pattern.compile(RegexConstants.REGEX_MOBILE_SIMPLE);
        PATTERN_ZIPCODE = Pattern.compile(REGEX_ZIPCODE);
    }

    public static enum PhoneType {
        /**
         * 手机
         */
        CELLPHONE,

        /**
         * 固定电话
         */
        FIXEDPHONE,

        /**
         * 非法格式号码
         */
        INVALIDPHONE
    }

    public static class Number {
        private PhoneType type;
        /**
         * 如果是手机号码，则该字段存储的是手机号码 前七位；如果是固定电话，则该字段存储的是区号
         */
        private String code;
        private String number;

        public Number(PhoneType _type, String _code, String _number) {
            this.type = _type;
            this.code = _code;
            this.number = _number;
        }

        public PhoneType getType() {
            return type;
        }

        public String getCode() {
            return code;
        }

        public String getNumber() {
            return number;
        }

        public String toString() {
            return String.format("[number:%s, type:%s, code:%s]", number, type.name(), code);
        }
    }

    /**
     * 判断是否为手机号码
     *
     * @param number 手机号码
     * @return
     */
    public static boolean isCellPhone(String number) {
        Matcher match = PATTERN_MOBILEPHONE.matcher(number);
        return match.matches();
    }

    /**
     * 判断是否为固定电话号码
     *
     * @param number 固定电话号码
     * @return
     */
    public static boolean isFixedPhone(String number) {
        Matcher match = PATTERN_FIXEDPHONE.matcher(number);
        return match.matches();
    }


    /**
     * 获取固定号码号码中的区号
     *
     * @param strNumber
     * @return
     */
    public static String getZipFromFixedPhone(String strNumber) {
        Matcher matcher = PATTERN_ZIPCODE.matcher(strNumber);
        if (matcher.find()) {
            return matcher.group(1);
        }

        return null;
    }

    /**
     * 获取字符串中的手机号码或座机
     *
     * @return
     */
    public static String getTelephoneNum(final String number) {

        if (number != null && number.length() > 0) {

            String num = getTelByLength(new Integer[]{10, 11, 12}, number);
            if (StringUtils.isNotBlank(num)) {
                return num;
            }
        }
        return number;
    }

    private static String getTelByLength(Integer[] integers, String number) {

        for (int i = 0; i < integers.length; i++) {
            Integer len = integers[i];
            if (number.length() >= len) {
                //先取后11位，如果是手机号码，则直接返回手机号码，如果不是，则返回原来的号码。
                String phoneNum = number.substring(number.length() - len, number.length());
                if (isCellPhone(phoneNum)) {
                    return phoneNum;
                }
                if (isFixedPhone(phoneNum)) {
                    return phoneNum;
                }
            }
        }
        return null;
    }


    /**
     * 检查号码类型，并获取号码前缀，手机获取前7位，固话获取区号
     *
     * @return
     */
    public static Number checkNumber(String _number) {
        String number = _number;
        Number rtNum = null;

        if (number != null && number.length() > 0) {
            if (isCellPhone(number)) {
                //如果手机号码以0开始，则去掉0
                if (number.charAt(0) == '0') {
                    number = number.substring(1);
                }

                rtNum = new Number(PhoneType.CELLPHONE, number.substring(0, 7), _number);
            } else if (isFixedPhone(number)) {
                //获取区号
                String zipCode = getZipFromFixedPhone(number);
                rtNum = new Number(PhoneType.FIXEDPHONE, zipCode, _number);
            } else {
                rtNum = new Number(PhoneType.INVALIDPHONE, null, _number);
            }
        }

        return rtNum;
    }

    private static String find(Pattern pattern, String num) {
        Matcher matcher = pattern.matcher(num);
        StringBuffer bf = new StringBuffer(64);
        while (matcher.find()) {
            bf.append(matcher.group());
        }
        return bf.toString();
    }

    /**
     * 判断数字是否是奇数
     *
     * @param num 号码
     * @return
     */
    public static boolean isOddNumber(int num) {
        return (num & 1) != 0;
    }

    /**
     * 数字转成以万为单位
     *
     * @param num
     * @return
     */
    public static String num2wanUnit(long num) {
        return num2wanUnit(Double.valueOf(num));
    }

    /**
     * 数字转成以万为单位
     *
     * @param num
     * @return
     */
    public static String num2wanUnit(double num) {
        String str = null;
        if (num < 10000) {
            str = String.valueOf(num);
        } else if (num < 100000000) {
            double n = (double) num / 10000;
            str = n + "万";
        } else if (num >= 100000000) {
            int m = (int) (num / 100000000);
            double a;
            a = num - m * 100000000;
            double i = (double) a / 10000;
            if (a == 0) {
                str = m + "亿";
                System.out.println("输入的数字为：" + m + "亿");
            } else {
                str = m + "亿" + i + "万";
            }
        }
        return str;
    }

    /**
     * 对总数进行分片。
     *
     * @param start    初始值
     * @param total    总量
     * @param splitter 分片的量
     * @return
     */
    public static long[][] splitLong(long start, long total, int splitter) {
        long per = (total - start) / splitter;
        long[] starts;
        long[] ends;
        if (per == 0) {
            starts = new long[]{start};
            ends = new long[]{total};
        } else {
            starts = new long[splitter];
            ends = new long[splitter];
            for (int i = 0, len = splitter; i < len; i++) {
                long size = start + i * per;
                starts[i] = size;

                //设置最后一个结束点的位置
                if (i == len - 1) {
                    ends[i] = total;
                } else {
                    ends[i] = size + per;
                }
            }
        }
        long[][] result = new long[][]{starts, ends};
        return result;
    }

}